package com.ymatou.ics.api.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.cs.api.service.CreateReturnApplyBillCall;
import com.ymatou.ics.api.service.AgreeApplyCall;
import com.ymatou.ics.api.service.RejectApplyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.AgreeApplyWapper;
import com.ymttest.database.sqlwapper.CreateReturnApplyBillWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*RejectApply
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RejectApply{

	private static RejectApplyCall rejectapplyCall;
	private static CreateReturnApplyBillCall createreturnapplybillCall;
	String DBiOrderId=null;   //获取数据库的订单号
	String RefundBillNo=null; //获取订单对应的退单号
	String iUserId=null;//获取买家 ID
	String iBuyerId=null;//获取买手 ID
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("RejectApply");
	}

	@Before
	public void caseUp() {
		
		rejectapplyCall=new RejectApplyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(rejectapplyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	//获取token  创建退货退款 
	public void CreateReturnApplyBillandToken(){
		
		Logger.debug("************************创建ToKen************************");
		//获取买家ToKen
		createreturnapplybillCall = new CreateReturnApplyBillCall("lunchzhao", "123456");			
		//获取订单号  规格  					
		CreateReturnApplyBillWapper createReturnApplyBillWapper = new CreateReturnApplyBillWapper();
    	Map m = new HashMap();
    	try{
	        List<Map> rm = createReturnApplyBillWapper.ioderidCatLogs();
	        int  iOrderId = (Integer) rm.get(0).get("iOrderId");
	        String  sCatalogId=(String)rm.get(0).get("sCatalogId");	
    
	        Logger.debug("************************创建退货退款************************");
	        //创建退货退款	        
			List<String> PicUrls=new ArrayList<>();
			PicUrls.add("http://pm2.img.ymatou.com/G01/upload/product/list/M00/07/14/rBBlD1YuBKKAJhsFAAZi-BfcdCc770_l.jpg");
			Map<String, Object> map = new HashMap<String, Object>();		
			map.put("OrderId",iOrderId);			    
			map.put("ReturnAmount",0.01);
			map.put("ReturnRemark","niras通过api创建的退款单-djj");
			map.put("ReturnReasonId",12);
			map.put("PicUrls",new JSONArray(PicUrls));
			map.put("products[0]->CatalogId",sCatalogId);
			map.put("products[0]->ReturnCount",1);
			//调用接口json文件
			JSONObject createreturnapplybill =  JsonBeanHelper.getJsonBean("/com.ymatou.cs.api.ReturnApplyBill/CreateReturnApplyBill/CreateReturnApplyBilltpl.json");
			createreturnapplybill = JsonBeanHelper.updateJsonBean(createreturnapplybill, map); 		
			JSONObject result = createreturnapplybillCall.callAndGetReturnData(createreturnapplybill);
			Logger.debug(result);
			String Status = result.getString("Status");
			String bCode = result.getString("bCode");
			if(Status.equals("200") && bCode.equals("0") ){
		   //通过订单找到对应的退单号与买家id 
			AgreeApplyWapper agreeApplyWapper = new AgreeApplyWapper();
	    	Map ma = new HashMap();
	    	ma.put("OrderId", iOrderId);	
	        List<Map> rmm = agreeApplyWapper.Tuidanid(ma); 
	        for(Map mapp : rmm)
	        {        
	            DBiOrderId= String.valueOf(mapp.get("iOrderId"));   //订单号
	        	//Logger.debug(DBiOrderId);  
	            RefundBillNo = String.valueOf(mapp.get("RefundBillNo"));  //退单号
	        	//Logger.debug(RefundBillNo);  
	        	iBuyerId = String.valueOf(mapp.get("iBuyerId"));   //买手ID 
	        	Logger.debug(iBuyerId);  	        	
	        }		
		}else{
			Logger.debug("创建退单失败原因为返回状态Status："+Status+"--》预期Status为：200；bCode为："+bCode+"--》预期为：0");					
		}	
    }catch(Exception e)
    	{
			Logger.fail(e);
		}
}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void TC_RejectApply_001() {
		Logger.start(true,"仅退款-卖家拒绝退款申请");
		try {
			 
			//创建退货退款订单
			CreateReturnApplyBillandToken();	
			//卖家拒绝退款申请
			Logger.debug("************************卖家拒绝退款申请************************"); 
			Map<String, Object> reject = new HashMap<String, Object>();		
			reject.put("UserId",iBuyerId);			    
			reject.put("BillId",RefundBillNo);
			reject.put("cause","测试M端-djj");

			//调用接口json文件
			JSONObject rejectapply =  JsonBeanHelper.getJsonBean("/com.ymatou.ics.api.agreeapply/RejectApply/RejectApplyTpl.json");
			rejectapply = JsonBeanHelper.updateJsonBean(rejectapply, reject); 		
			JSONObject result1 = rejectapplyCall.callAndGetReturnData(rejectapply);	 
			Logger.debug(result1);		
			
			//断言
			String Code = result1.getString("Code");
			Logger.verifyIsContains(Code, 200, "\"Code\":200");				
			String BCode=result1.getString("BCode");
			Logger.verifyIsContains(BCode, 0,  "\"BCode\":0");				
			String ReturnBillId=result1.getJSONObject("Data").getString("ReturnBillId");
			Logger.verifyEquals(RefundBillNo, ReturnBillId, "传入的退单号与返回的运单号一致！");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}